package com.wei.czz.common.property;

import com.wei.czz.common.enums.admin.user.RequestChannelEnum;
import com.wei.czz.common.tool.BaseEntity;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2025-04-08 22:47:46
 * className: AccessTokenProperty
 * version: 1.0
 * description:
 */
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "token")
@Validated
public class AccessTokenProperty extends BaseEntity {

    /**
     * web端令牌配置
     */
    @NotNull(message = "web端令牌配置不能为空")
    @Valid
    private TokenProperty web;


    /**
     * 获取访问令牌过期时间
     *
     * @param requestChannelEnum 登录渠道枚举对象
     * @return 令牌过期时间
     */
    public Long getAccessTokenExpire(RequestChannelEnum requestChannelEnum) {
        switch (requestChannelEnum) {
            case WEB:
                return web.getAccessTokenExpire() * 1000;
            default:
                return 0L;
        }
    }

    /**
     * 获取刷新令牌过期时间
     *
     * @param requestChannelEnum 登录渠道枚举对象
     * @return 令牌过期时间
     */
    public Long getRefreshTokenExpire(RequestChannelEnum requestChannelEnum) {
        switch (requestChannelEnum) {
            case WEB:
                return web.getRefreshTokenExpire() * 1000;
            default:
                return 0L;
        }
    }

    @Getter
    @Setter
    public static class TokenProperty extends BaseEntity {

        /**
         * 访问令牌过期时间（单位：秒）
         */
        @NotNull(message = "访问令牌过期时间不能为空")
        private Long accessTokenExpire;

        /**
         * 刷新令牌过期时间（单位：秒）
         */
        @NotNull(message = "刷新令牌过期时间不能为空")
        private Long refreshTokenExpire;

    }
}
